package Image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.01.20.23.16
 */
public class Im1 {
    public static void main(String[] args) throws IOException {
        //建立缓冲图像
        var i = new BufferedImage(400, 400, 2);
        //根据缓冲图像，建立画笔 2D
        var g = i.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        //设置颜色
        g.setColor(new Color(255, 0, 0, 100));
        //画一个矩形，用设置的颜色填充
        g.fillRect(0, 0, 400, 400);

        //画直线
        g.setStroke(new BasicStroke(2));
        g.setColor(Color.YELLOW);
        g.setColor(new Color(0xff, 0xff, 0x00));
        g.drawLine(0, 0, 400, 400);
        g.drawLine(400, 0, 0, 400);

        //控制画的粗细
        g.setStroke(new BasicStroke(5));
        Random rand = new Random();
        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
        g.drawLine(0, 200, 400, 200);
        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
        g.drawLine(200, 0, 200, 400);

        //画矩形（空，填充）
        g.setStroke(new BasicStroke(1));
        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
        g.drawRect(50, 50, 300, 300);

        //填充矩形，没有线条
        g.setColor(new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), 150));
        g.fillRect(100, 150, 200, 100);


        //释放画笔占用内在
        g.dispose();
        //将再好的缓冲图像，输出到硬盘上 "png" 图像格式 new File("") 图像的位置和文件名称
        ImageIO.write(i, "png", new File("img02.png"));

        //调用系统命令，打开保存好的图像
        var run = Runtime.getRuntime();
        run.exec("cmd /k start img02.png");
    }

    public static void one(String[] args) throws IOException {
        var width = 300;
        var height = 280;
        //1 真彩 2 透明度 10 灰度
        var img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        ImageIO.write(img, "jpg", new File("img01.jpg"));

    }
}
